%% Performance Evaluation of EW UEP RLNC Coding Scheme
%
% Author: 12GR652
% Last modified: 26-04-2012
%
% Description:
% This script drives the expanding windows random linear network coding
% simulation in order to plot results. A plot is saved as
% 'ewrlnc_plot.eps'and a logfile as 'ewrlnc_log.txt'
%
% Speed:
%   - Note that ewrlnc(... ) is very very slow - compute with power :)
%

%% Startup

% Clear workspace
close all
clear all
clc

% Note time (performance)
tic

% Prepare logfile
the_time_now=datestr(now);
filename_log=strcat(the_time_now,'_ewrlnc_log.txt');
filename_fig=strcat(the_time_now,'_ewrlnc_log.eps');
diary(filename_log)
diary on

disp('Testing Expanding Windows RLNC');

%% Parameters to test EWRLNC with:

m=1; % field parameter (2^m = field size), we shall use either 1 or 8
nb_sims=10; % simulations pr nb_transmissions
R1=0.5; % probability of layer 1
R2=0.25; % Probability of layer 2
R3=0.25; % probability of layyer 3

I_pkts=3; % Nb of packets in I block
P1_pkts=3;% Nb of packets in P1 block
P2_pkts=3; % Nb of packkets in P2 block
gen_size=I_pkts+P1_pkts+P2_pkts; % Generation size

%% Save configuration to logfile

disp('Field parameter (2^m = field size), we shall use either 1 or 8');
disp(m);
disp('Simulations pr nb_transmissions');
disp(nb_sims);
disp('Probability of transmitting layer 1');
disp(R1);
disp('Probability of transmitting layer 2');
disp(R2);
disp('Probability of transmitting layer 3');
disp(R3);
disp('Nb of packets in I block');
disp(I_pkts);
disp('Nb of packets in P1 block');
disp(P1_pkts);
disp('Nb of packets in P2 block');
disp(P2_pkts);
disp('Generation size');
disp(gen_size);

%% Collect data

I_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding I
P1_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding P1
P2_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding P2

% For pkt_loss=0 and increasing number of packets send, collect data
% Send until 3 x generation size has been send
for nb_transmissions = 1:1:(3*gen_size)
    [I,P1,P2]=ewrlnc(nb_sims,I_pkts,P1_pkts,P2_pkts,m,nb_transmissions,R1,R2,R3);
    I_prob(nb_transmissions)=I/nb_sims;
    P1_prob(nb_transmissions)=P1/nb_sims;
    P2_prob(nb_transmissions)=P2/nb_sims;
end

% Evaluate for EEP (we can tweak our UEP to EEP, by R1=0, R2=0, R3=1)

EEP_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding P2(all pkts)

for nb_transmissions = 1:1:(3*gen_size)
    [I,P1,P2]=ewrlnc(nb_sims,I_pkts,P1_pkts,P2_pkts,m,nb_transmissions,0,0,1);
    EEP_prob(nb_transmissions)=P2/nb_sims;
end

%% Plotting

% quick fix of x vector - must be the same sequence as in for-loop
nb_transmissions = 1:1:(3*gen_size);

% Suppress graphic output (better for terminal execution)
figure('visible','off');

% Fill plot with data
hold on
plot(nb_transmissions,I_prob,'-*','Color','black');
plot(nb_transmissions,P1_prob,'-o','Color','black');
plot(nb_transmissions,P2_prob,'-d','Color','black');
plot(nb_transmissions,EEP_prob,'--','Color','black');
hold off

% Plot Annotation
xlabel('Received Packets [-]')
ylabel('Probability of Decoding [-]')
legend('I','P1','P2','EEP','location','SouthEast')
grid('on')
pbaspect([2.5 1 1])
set(gca,'XTick',0:1:3*gen_size)
xlim([0 3*gen_size])

% Finish file and 
print(gcf,filename_fig)
disp('It took [s]')
disp(toc)
disp('This is the end...')
diary off





